package com.starchen.optimalselection.product.controller;

import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.Result;
import com.starchen.optimalselection.product.model.form.AttributeGroupForm;
import com.starchen.optimalselection.product.model.vo.GroupWithAttrVo;
import com.starchen.optimalselection.product.service.AttributeGroupService;
import com.starchen.optimalselection.common.model.AttributeGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *  * 属性分组
 * @author daxilong
 * @date 2024/11/27 19:57
 */
@Api(tags = "商品属性分组管理")
@RestController
@RequestMapping("admin/group")
public class AttributeGroupController {

    @Autowired
    private AttributeGroupService attrGroupService;

    @ApiOperation("分页获取所有商品属性分类")
    @GetMapping("/page")
    public Result page(AttributeGroupForm params){
        ResultPage page = attrGroupService.queryPage(params);
        return Result.success(page);
    }

    /**
     * 根据category查询所有的分组信息
     */
    @GetMapping("/list")
    public Result list(Long categoryId){
        List<AttributeGroup> attrGroups = attrGroupService.getGroupByCate(categoryId);
        return Result.success(attrGroups);
    }

    /**
     * 根据category查询所有的分组信息
     */
    @GetMapping("/all")
    public Result allGroup(){
        List<AttributeGroup> attrGroups = attrGroupService.getAllGroup();
        return Result.success(attrGroups);
    }

    @ApiOperation("获取单个商品属性分组信息")
    @GetMapping("/info")
    public Result info(Long id){
        AttributeGroup attrGroup = attrGroupService.getAttrGroupInfo(id);
        return Result.success(attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/create")
    public Result save(@RequestBody AttributeGroup attrGroup){
        attrGroupService.save(attrGroup);
        return Result.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody AttributeGroup attrGroup){
        attrGroupService.updateById(attrGroup);

        return Result.success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        attrGroupService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @GetMapping("/withattr")
    public Result getAttrGroupWithAttrs(Long categoryId){
        List<GroupWithAttrVo> vos =  attrGroupService.getGroupWithAttrs(categoryId);
        return Result.success(vos);
    }

}

